/**
 * jims
 */
package com.jims.master.modules.template.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.sys.utils.UserUtils;
import com.jims.master.modules.template.entity.DoctorTempla;
import com.jims.master.modules.template.entity.TreatRecordItem;
import com.jims.master.modules.template.service.DoctorTemplaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 * @author zp
 * @version 2017-06-02
 */
@Controller
@RequestMapping(value = "${adminPath}/template/doctorTempla")
public class TreatTemplaController extends BaseController {

	@Autowired
	private DoctorTemplaService doctorTemplaService;
	
	@ModelAttribute
	public DoctorTempla get(@RequestParam(required=false) String id) {
		DoctorTempla entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = doctorTemplaService.get(id);
		}
		if (entity == null){
			entity = new DoctorTempla();
		}
		return entity;
	}

	/**
	 * 治疗模板主页
	 * @param doctorTempla
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"indexTreat", ""})
	public String list(DoctorTempla doctorTempla,String type, TreatRecordItem treatRecordItem, HttpServletRequest request, HttpServletResponse response, Model model) {
		UserUtils userUtils = new UserUtils();
		doctorTempla.setCreatr(userUtils.getUser().getId());
		doctorTempla.setProjectType(type);
		List<DoctorTempla> page = doctorTemplaService.findList(doctorTempla);
//		model.addAttribute("creatr",userUtils.getUser().getId());
		model.addAttribute("pageApply", page);
		if("0".equals(type)){
			return "modules/template/treatTemplate/treatTemplaIndex";
		} else if("1".equals(type)){
			return "modules/template/orderTemplate/orderTemplaIndex";
		} else if("2".equals(type)){
			return "modules/template/prescTemplate/prescTemplaIndex";
		}  else {
			model.addAttribute("treatRecordItem", treatRecordItem);
			return "modules/template/recordTemplate/recordTemplaIndex";
		}
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "formTreat")
	public String formTreat(DoctorTempla doctorTempla, String type,Model model) {
		model.addAttribute("doctorTempla", doctorTempla);
		if("0".equals(type)){
			return "modules/template/treatTemplate/treatTemplaForm";
		} else if("1".equals(type)){
			return "modules/template/orderTemplate/orderTemplaForm";
		} else if("2".equals(type)){
			return "modules/template/prescTemplate/prescTemplaForm";
		} else {
			return "modules/template/recordTemplate/recordTemplaForm";
		}
	}


	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DoctorTempla doctorTempla, Model model) {
	    StringData data=new StringData();
		int i=doctorTemplaService.save(doctorTempla);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DoctorTempla doctorTempla) {
	    StringData data=new StringData();
		int i=doctorTemplaService.delete(doctorTempla);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}